#include "printercontrol.h"
#include <QMutexLocker>

// 初始化静态成员变量（单例核心）
PrinterControl* PrinterControl::m_instance = nullptr;
QMutex PrinterControl::m_mutex;

// 私有构造函数实现
PrinterControl::PrinterControl(QObject *parent)
    : QObject{parent},
    m_CurrentType(0),  // 初始化打印机类型默认值
    m_Language_Type(false)  // 初始化语言默认值（例如：false为英文，true为中文）
{
    m_Printer = new QSerialPort(this);
}

// 析构函数实现
PrinterControl::~PrinterControl()
{
    // 确保串口关闭并释放资源
    if (m_Printer->isOpen()) {
        m_Printer->close();
    }
    m_Printer->deleteLater();
}

// 单例实例获取方法（线程安全）
PrinterControl* PrinterControl::getInstance(QObject *parent)
{
    // 双重检查锁定，保证线程安全且高效
    if (!m_instance) {
        QMutexLocker locker(&m_mutex);  // 自动加锁/解锁，避免死锁
        if (!m_instance) {
            m_instance = new PrinterControl(parent);
        }
    }
    return m_instance;
}

// 打印机连接参数设置（原有实现保持不变）
void PrinterControl::setPrinterConnectParameter(QString serialPort,
                                                QString baudRate,
                                                QString dataBit,
                                                QString parityBit,
                                                QString stopBit)
{
    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBit;
    QSerialPort::Parity   set_ParityBit;
    QSerialPort::StopBits set_StopBit;

    // 设置波特率
    if("115200" == baudRate){
        set_BaudRate = QSerialPort::Baud115200;
    }else if("57600" == baudRate){
        set_BaudRate = QSerialPort::Baud57600;
    }else if("38400" == baudRate){
        set_BaudRate = QSerialPort::Baud38400;
    }else if("19200" == baudRate){
        set_BaudRate = QSerialPort::Baud19200;
    }else if("9600" == baudRate){
        set_BaudRate = QSerialPort::Baud9600;
    }else if("4800" == baudRate){
        set_BaudRate = QSerialPort::Baud4800;
    }else if("2400" == baudRate){
        set_BaudRate = QSerialPort::Baud2400;
    }else {
        set_BaudRate = QSerialPort::Baud1200;
    }

    // 设置数据位
    if("8" == dataBit){
        set_DataBit = QSerialPort::Data8;
    }else if("7" == dataBit){
        set_DataBit = QSerialPort::Data7;
    }else if("6" == dataBit){
        set_DataBit = QSerialPort::Data6;
    }else {
        set_DataBit = QSerialPort::Data5;
    }

    // 设置校验位
    if("None" == parityBit){
        set_ParityBit = QSerialPort::NoParity;
    }else if("Even" == parityBit){
        set_ParityBit = QSerialPort::EvenParity;
    }else if("Odd" == parityBit){
        set_ParityBit = QSerialPort::OddParity;
    }else if("Mark" == parityBit){
        set_ParityBit = QSerialPort::MarkParity;
    }else {
        set_ParityBit = QSerialPort::SpaceParity;
    }

    // 设置停止位
    if("1" == stopBit){
        set_StopBit = QSerialPort::OneStop;
    }else if("1.5" == stopBit){
        set_StopBit = QSerialPort::OneAndHalfStop;
    }else{
        set_StopBit = QSerialPort::TwoStop;
    }

    m_Printer->setPortName(serialPort);
    m_Printer->setBaudRate(set_BaudRate);
    m_Printer->setDataBits(set_DataBit);
    m_Printer->setParity(set_ParityBit);
    m_Printer->setStopBits(set_StopBit);
    // 显式设置流控制（默认无，增加可读性）
    m_Printer->setFlowControl(QSerialPort::NoFlowControl);
}

// 打印动作触发（原有实现保持不变）
void PrinterControl::printerActive(QString MaterialHeight,
                                   QString MaterialLength,
                                   QString LeftAngle,
                                   QString RightAngle)
{
    if(CurrentType() == 1)
    {
        if(Language_Type())
        {
            qDebug()<<"打印动作 - 瑞工汉语";
            print_Active_RuiGong(MaterialHeight,
                                 MaterialLength,
                                 LeftAngle,
                                 RightAngle,
                                 MaterialName(),
                                 Customer_Name());
        }
        else
        {
            qDebug()<<"打印动作 - 瑞工英语";
            print_Active_RuiGong_en_US(MaterialHeight,
                                       MaterialLength,
                                       LeftAngle,
                                       RightAngle,
                                       MaterialName(),
                                       Customer_Name());
        }
    }
    else if(CurrentType() == 2)
    {
        if(Language_Type())
        {
            qDebug()<<"打印动作 - 炜煌汉语";
            print_Active_WeiHuang(MaterialHeight,
                                  MaterialLength,
                                  LeftAngle,
                                  RightAngle,
                                  MaterialName(),
                                  Customer_Name());
        }
        else
        {
            qDebug()<<"打印动作 - 炜煌英语";
            print_Active_WeiHuang_en_US(MaterialHeight,
                                        MaterialLength,
                                        LeftAngle,
                                        RightAngle,
                                        MaterialName(),
                                        Customer_Name());
        }
    }
    else if(CurrentType() == 3)
    {
        if(Language_Type())
        {
            qDebug()<<"打印动作 - 达普汉语";
            print_Active_DaPu(MaterialHeight,
                              MaterialLength,
                              LeftAngle,
                              RightAngle,
                              MaterialName(),
                              Customer_Name());
        }
        else
        {
            qDebug()<<"打印动作 - 达普英语";
            print_Active_DaPu_en_US(MaterialHeight,
                                    MaterialLength,
                                    LeftAngle,
                                    RightAngle,
                                    MaterialName(),
                                    Customer_Name());
        }
    }
}

// 炜煌打印机汉语打印（原有实现保持不变）
void PrinterControl::print_Active_WeiHuang(QString MaterialHeight,
                                           QString MaterialLength,
                                           QString LeftAngle,
                                           QString RightAngle,
                                           QString MaterialName,
                                           QString Customer)
{
    QByteArray printResult;

    QString printWordSet = QString::fromLocal8Bit("1B401B6330021B4C1B54001B57000000008001640118");
    QString printMethod = QString::fromLocal8Bit("0C");

    QString printWord01Set = QString::fromLocal8Bit("1B2400001D240000");
    QString printWord02Set = QString::fromLocal8Bit("1B2400001D241E00");
    QString printWord03Set = QString::fromLocal8Bit("1B2400001D243C00");
    QString printWord04Set = QString::fromLocal8Bit("1B2400001D245A00");
    QString printWord05Set = QString::fromLocal8Bit("1B2400001D247800");
    QString printWord06Set = QString::fromLocal8Bit("1B2400001D249600");
    QString printWord07Set = QString::fromLocal8Bit("1B2400001D24B400");
    QString printWord08Set = QString::fromLocal8Bit("1B2400001D24D200");

    QString recvQmlLength = QString("长度:%1\n").arg(MaterialLength);
    QString recvQmlHeight = QString("高度:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("左角度:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("右角度:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("%1\n").arg(MaterialName);
    QString recvQmlMaterialName = QString("%1\n").arg(Customer);

    QString handleLength = recvQmlLength.toLocal8Bit().toHex();
    QString handleHeight = recvQmlHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString costomTitle = QString("客户名称:").toLocal8Bit().toHex();
    QString materialTitle = QString("型材名称:").toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength,2,16,QLatin1Char('0'));
    QString printerQRCodeSet = QString::fromLocal8Bit("1B24C8001D2400001B5A000005%1%2").arg(QR_ParameterLength).arg("00");

    printResult = printWordSet.toUtf8() +
                  printWord01Set.toUtf8() + handleLength.toUtf8() +
                  printWord02Set.toUtf8() + handleHeight.toUtf8() +
                  printWord03Set.toUtf8() + handleLeftAngle.toUtf8() +
                  printWord04Set.toUtf8() + handleRightAngle.toUtf8() +
                  printWord05Set.toUtf8() + costomTitle.toUtf8() +
                  printWord06Set.toUtf8() + handleCustomName.toUtf8() +
                  printWord07Set.toUtf8() + materialTitle.toUtf8() +
                  printWord08Set.toUtf8() + handleMaterialName.toUtf8() +
                  printerQRCodeSet.toUtf8() + handleParameter.toUtf8() +
                  printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"炜煌打印机 - 发送成功";
    }
    else
    {
        qDebug()<<"炜煌打印机 - 发送失败";
    }
}

// 瑞工打印机汉语打印（原有实现保持不变）
void PrinterControl::print_Active_RuiGong(QString MaterialHeight,
                                          QString MaterialLength,
                                          QString LeftAngle,
                                          QString RightAngle,
                                          QString MaterialName,
                                          QString Customer)
{
    QByteArray printResult;

    QString printerMechineSet = QString::fromLocal8Bit("1B401B63001B732B40");

    QString recvParameter = QString("%1,%2,%3,%4").arg(MaterialLength).arg(MaterialHeight).arg(LeftAngle).arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength,2,16,QLatin1Char('0'));

    QString QR_print = QString::fromLocal8Bit("1D6CDC0032001D57041D6E200601%1%2").arg(handleParameter).arg("00");

    QString printWordSet = QString::fromLocal8Bit("1B2400001B57010D");
    QString printMethod = QString::fromLocal8Bit("1B69");

    QString recvQmlmaterialLength = QString("长度:%1\n").arg(MaterialLength);
    QString recvQmlmaterialHeight = QString("高度:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("左角度:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("右角度:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("客户名称:\n%1\n").arg(Customer);
    QString recvQmlMaterialName = QString("型材名称:\n%1\n").arg(MaterialName);

    QString handlmaterialLength = recvQmlmaterialLength.toLocal8Bit().toHex();
    QString handlematerialHeight = recvQmlmaterialHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    printResult = printerMechineSet.toUtf8() +
                  QR_print.toUtf8() +
                  printWordSet.toUtf8() +
                  handlmaterialLength.toUtf8() +
                  handlematerialHeight.toUtf8() +
                  handleLeftAngle.toUtf8() +
                  handleRightAngle.toUtf8() +
                  handleCustomName.toUtf8() +
                  handleMaterialName.toUtf8() +
                  printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"瑞工打印机 - 发送成功";
    }
    else
    {
        qDebug()<<"瑞工打印机 - 发送失败";
    }
}

// 达普打印机汉语打印（原有实现保持不变）
void PrinterControl::print_Active_DaPu(QString MaterialHeight, QString MaterialLength, QString LeftAngle, QString RightAngle, QString MaterialName, QString Customer)
{
    QByteArray printResult;

    QByteArray printerInit = (QString("%1%2%3%4").arg("1A5B0100000000").arg("B801").arg("0001").arg("00")).toLocal8Bit();

    QByteArray MaterialHeight_Out = (QString("1A54010000050000600011%1%2%3").arg("D0CDB2C4B8DFB6C83A").arg(MaterialHeight.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray MaterialLength_Out = (QString("1A54010000230000600011%1%2%3").arg("D0CDB2C4B3A4B6C83A").arg(MaterialLength.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray LeftAngle_Out = (QString("1A54010000410000600011%1%2%3").arg("D7F3BDC7B6C83A").arg(LeftAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray RightAngle_Out = (QString("1A540100005F0000600011%1%2%3").arg("D3D2BDC7B6C83A").arg(RightAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray MaterialName_Out1 = (QString("1A540100007D0000600011%1%2").arg("D0CDB2C4C3FBB3C63A").arg("00")).toLocal8Bit();
    QByteArray MaterialName_Out2 = (QString("1A540100009B0000600011%1%2").arg(MaterialName.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray Customer_Out1 = (QString("1A54010000B90000600011%1%2").arg("BFCDBBA7C3FBB3C63A").arg("00")).toLocal8Bit();
    QByteArray Customer_Out2 = (QString("1A54010000D70000600011%1%2").arg(Customer.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();

    QByteArray QR_Code_Init = (QString("%1%2%3%4%5%6%7").arg("1A3100").arg("04").arg("04").arg("C800").arg("4000").arg("05").arg("00")).toLocal8Bit();
    QByteArray QR_Code_Data = (QString("%1%2%3%4%5%6%7%8").arg(MaterialHeight.toLocal8Bit().toHex()).arg("2C").arg(MaterialLength.toLocal8Bit().toHex()).arg("2C").arg(LeftAngle.toLocal8Bit().toHex()).arg("2C").arg(RightAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();

    QByteArray print = (QString("%1").arg("1A5D001A4F00")).toLocal8Bit();

    printResult =  printerInit+
                  MaterialHeight_Out+
                  MaterialLength_Out+
                  LeftAngle_Out+
                  RightAngle_Out+
                  MaterialName_Out1+
                  MaterialName_Out2+
                  Customer_Out1 +
                  Customer_Out2 +
                  QR_Code_Init+
                  QR_Code_Data+
                  print;

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"达普打印机 - 发送成功";
    }
    else
    {
        qDebug()<<"达普打印机 - 发送失败";
    }
}

// 炜煌打印机英语打印（原有实现保持不变）
void PrinterControl::print_Active_WeiHuang_en_US(QString MaterialHeight,
                                                 QString MaterialLength,
                                                 QString LeftAngle,
                                                 QString RightAngle,
                                                 QString MaterialName,
                                                 QString Customer)
{
    QByteArray printResult;

    QString printWordSet = QString::fromLocal8Bit("1B401B6330021B4C1B54001B57000000008001640118");
    QString printMethod = QString::fromLocal8Bit("0C");

    QString printWord01Set = QString::fromLocal8Bit("1B2400001D240000");
    QString printWord02Set = QString::fromLocal8Bit("1B2400001D241E00");
    QString printWord03Set = QString::fromLocal8Bit("1B2400001D243C00");
    QString printWord04Set = QString::fromLocal8Bit("1B2400001D245A00");
    QString printWord05Set = QString::fromLocal8Bit("1B2400001D247800");
    QString printWord06Set = QString::fromLocal8Bit("1B2400001D249600");
    QString printWord07Set = QString::fromLocal8Bit("1B2400001D24B400");
    QString printWord08Set = QString::fromLocal8Bit("1B2400001D24D200");

    QString recvQmlLength = QString("Length:%1\n").arg(MaterialLength);
    QString recvQmlHeight = QString("Height:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("L_Angle:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("R_Angle:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("%1\n").arg(MaterialName);
    QString recvQmlMaterialName = QString("%1\n").arg(Customer);

    QString handleLength = recvQmlLength.toLocal8Bit().toHex();
    QString handleHeight = recvQmlHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString costomTitle = QString("Custom_Name:").toLocal8Bit().toHex();
    QString materialTitle = QString("Material_Name:").toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength,2,16,QLatin1Char('0'));
    QString printerQRCodeSet = QString::fromLocal8Bit("1B24C8001D2400001B5A000005%1%2").arg(QR_ParameterLength).arg("00");

    printResult = printWordSet.toUtf8() +
                  printWord01Set.toUtf8() + handleLength.toUtf8() +
                  printWord02Set.toUtf8() + handleHeight.toUtf8() +
                  printWord03Set.toUtf8() + handleLeftAngle.toUtf8() +
                  printWord04Set.toUtf8() + handleRightAngle.toUtf8() +
                  printWord05Set.toUtf8() + costomTitle.toUtf8() +
                  printWord06Set.toUtf8() + handleCustomName.toUtf8() +
                  printWord07Set.toUtf8() + materialTitle.toUtf8() +
                  printWord08Set.toUtf8() + handleMaterialName.toUtf8() +
                  printerQRCodeSet.toUtf8() + handleParameter.toUtf8() +
                  printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"炜煌打印机_en_US - 发送成功";
    }
    else
    {
        qDebug()<<"炜煌打印机_en_US - 发送失败";
    }
}

// 瑞工打印机英语打印（原有实现保持不变）
void PrinterControl::print_Active_RuiGong_en_US(QString MaterialHeight,
                                                QString MaterialLength,
                                                QString LeftAngle,
                                                QString RightAngle,
                                                QString MaterialName,
                                                QString Customer)
{
    QByteArray printResult;

    QString printerMechineSet = QString::fromLocal8Bit("1B401B63001B732B40");

    QString recvParameter = QString("%1,%2,%3,%4").arg(MaterialLength).arg(MaterialHeight).arg(LeftAngle).arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength,2,16,QLatin1Char('0'));

    QString QR_print = QString::fromLocal8Bit("1D6CDC0032001D57041D6E200601%1%2").arg(handleParameter).arg("00");

    QString printWordSet = QString::fromLocal8Bit("1B2400001B57010D");
    QString printMethod = QString::fromLocal8Bit("1B69");

    QString recvQmlmaterialLength = QString("Length:%1\n").arg(MaterialLength);
    QString recvQmlmaterialHeight = QString("Height:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("L_Right:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("R_Right:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("Custom_Name:\n%1\n").arg(Customer);
    QString recvQmlMaterialName = QString("Material_Name:\n%1\n").arg(MaterialName);

    QString handlmaterialLength = recvQmlmaterialLength.toLocal8Bit().toHex();
    QString handlematerialHeight = recvQmlmaterialHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    printResult = printerMechineSet.toUtf8() +
                  QR_print.toUtf8() +
                  printWordSet.toUtf8() +
                  handlmaterialLength.toUtf8() +
                  handlematerialHeight.toUtf8() +
                  handleLeftAngle.toUtf8() +
                  handleRightAngle.toUtf8() +
                  handleCustomName.toUtf8() +
                  handleMaterialName.toUtf8() +
                  printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"瑞工打印机_en_US - 发送成功";
    }
    else
    {
        qDebug()<<"瑞工打印机_en_US - 发送失败";
    }
}

// 达普打印机英语打印（原有实现保持不变）
void PrinterControl::print_Active_DaPu_en_US(QString MaterialHeight,
                                             QString MaterialLength,
                                             QString LeftAngle,
                                             QString RightAngle,
                                             QString MaterialName,
                                             QString Customer)
{
    QByteArray printResult;

    QByteArray printerInit = (QString("%1%2%3%4").arg("1A5B0100000000").arg("B801").arg("0001").arg("00")).toLocal8Bit();

    QByteArray MaterialHeight_Out = (QString("1A54010000050000600011%1%2%3").arg("4D6174657269616C5F4865696768743A").arg(MaterialHeight.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray MaterialLength_Out = (QString("1A54010000230000600011%1%2%3").arg("4D6174657269616C5F4C656E6774683A").arg(MaterialLength.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray LeftAngle_Out = (QString("1A54010000410000600011%1%2%3").arg("4C656674416E676C653A").arg(LeftAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray RightAngle_Out = (QString("1A540100005F0000600011%1%2%3").arg("5269676874416E676C653A").arg(RightAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray MaterialName_Out1 = (QString("1A540100007D0000600011%1%2").arg("4D6174657269616C5F4E616D653A").arg("00")).toLocal8Bit();
    QByteArray MaterialName_Out2 = (QString("1A540100009B0000600011%1%2").arg(MaterialName.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();
    QByteArray Customer_Out1 = (QString("1A54010000B90000600011%1%2").arg("437573746F6D5F4E616D653A").arg("00")).toLocal8Bit();
    QByteArray Customer_Out2 = (QString("1A54010000D70000600011%1%2").arg(Customer.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();

    QByteArray QR_Code_Init = (QString("%1%2%3%4%5%6%7").arg("1A3100").arg("04").arg("04").arg("C800").arg("4000").arg("05").arg("00")).toLocal8Bit();
    QByteArray QR_Code_Data = (QString("%1%2%3%4%5%6%7%8").arg(MaterialHeight.toLocal8Bit().toHex()).arg("2C").arg(MaterialLength.toLocal8Bit().toHex()).arg("2C").arg(LeftAngle.toLocal8Bit().toHex()).arg("2C").arg(RightAngle.toLocal8Bit().toHex()).arg("00")).toLocal8Bit();

    QByteArray print = (QString("%1").arg("1A5D001A4F00")).toLocal8Bit();

    printResult =  printerInit+
                  MaterialHeight_Out+
                  MaterialLength_Out+
                  LeftAngle_Out+
                  RightAngle_Out+
                  MaterialName_Out1+
                  MaterialName_Out2+
                  Customer_Out1 +
                  Customer_Out2 +
                  QR_Code_Init+
                  QR_Code_Data+
                  print;

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if(m_Printer->write(sendMasage))
    {
        qDebug()<<"达普打印机_en_US - 发送成功";
    }
    else
    {
        qDebug()<<"达普打印机_en_US - 发送失败";
    }
}

// 连接打印机设备（原有实现优化）
void PrinterControl::connectToDevice()
{
    // 先关闭已打开的连接，避免重复连接
    if (m_Printer->isOpen()) {
        m_Printer->close();
    }

    if(m_Printer->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Cpp: 打印机 连接成功，端口：" << m_Printer->portName();
    }
    else
    {
        qDebug()<<"Cpp: 打印机 连接失败，错误：" << m_Printer->errorString();
    }
}

// 属性访问器实现（原有实现保持不变）
int PrinterControl::CurrentType() const
{
    return m_CurrentType;
}

void PrinterControl::setCurrentType(int newCurrentType)
{
    if (m_CurrentType == newCurrentType)
        return;
    m_CurrentType = newCurrentType;
    emit CurrentTypeChanged();
}

QString PrinterControl::MaterialHeight() const
{
    return m_MaterialHeight;
}

void PrinterControl::setMaterialHeight(const QString &newMaterialHeight)
{
    if (m_MaterialHeight == newMaterialHeight)
        return;
    m_MaterialHeight = newMaterialHeight;
    emit MaterialHeightChanged();
}

QString PrinterControl::MaterialLength() const
{
    return m_MaterialLength;
}

void PrinterControl::setMaterialLength(const QString &newMaterialLength)
{
    if (m_MaterialLength == newMaterialLength)
        return;
    m_MaterialLength = newMaterialLength;
    emit MaterialLengthChanged();
}

QString PrinterControl::LeftAngle() const
{
    return m_LeftAngle;
}

void PrinterControl::setLeftAngle(const QString &newLeftAngle)
{
    if (m_LeftAngle == newLeftAngle)
        return;
    m_LeftAngle = newLeftAngle;
    emit LeftAngleChanged();
}

QString PrinterControl::RightAngle() const
{
    return m_RightAngle;
}

void PrinterControl::setRightAngle(const QString &newRightAngle)
{
    if (m_RightAngle == newRightAngle)
        return;
    m_RightAngle = newRightAngle;
    emit RightAngleChanged();
}

QString PrinterControl::MaterialName() const
{
    return m_MaterialName;
}

void PrinterControl::setMaterialName(const QString &newMaterialName)
{
    if (m_MaterialName == newMaterialName)
        return;
    m_MaterialName = newMaterialName;
    emit MaterialNameChanged();
}

QString PrinterControl::Customer_Name() const
{
    return m_Customer_Name;
}

void PrinterControl::setCustomer_Name(const QString &newCustomer_Name)
{
    if (m_Customer_Name == newCustomer_Name)
        return;
    m_Customer_Name = newCustomer_Name;
    emit Customer_NameChanged();
}

void PrinterControl::on_AutoActiveSlots(bool left, bool right, QString materialLength, QString materialHeight)
{
    QString LeftAngle;
    QString RightAngle;

    if(left)
    {
        LeftAngle = "90";
    }
    else
    {
        LeftAngle = "45";
    }

    if(right)
    {
        RightAngle = "90";
    }
    else
    {
        RightAngle = "45";
    }

    // if(CurrentType() == 1)
    // {
    //     print_Active_RuiGong(materialHeight,materialLength,LeftAngle,RightAngle,MaterialName(),Customer_Name());
    // }
    // else if(CurrentType() == 2)
    // {
    //     print_Active_WeiHuang(materialHeight,materialLength,LeftAngle,RightAngle,MaterialName(),Customer_Name());
    // }
    // else if(CurrentType() == 3)
    // {
    //     print_Active_DaPu(materialHeight,materialLength,LeftAngle,RightAngle,MaterialName(),Customer_Name());
    // }

    printerActive(materialHeight,materialLength,LeftAngle,RightAngle);

}

bool PrinterControl::Language_Type() const
{
    return m_Language_Type;
}

void PrinterControl::setLanguage_Type(bool newLanguage_Type)
{
    if (m_Language_Type == newLanguage_Type)
        return;
    m_Language_Type = newLanguage_Type;
    emit Language_TypeChanged();
}
